using System;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
namespace CGF
{
    public partial class DatePicker : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        private Action<string> onConfirm;
        private Action onClose;
        private float dialogOutY = -720;
        private int fromYear = 2020;
        private int toYear = 2050;

        public static void Open(Action<string> onConfirm, Action onClose)
        {
            var datePicker = UIManagerComponent.instance.OpenUI<DatePicker>(UILayer.Normal, RootComponent.instance, true);
            datePicker.onConfirm = onConfirm;
            datePicker.onClose = onClose;
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<DatePicker>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/Common/DatePicker", layer, isFloat);
            BindProperties(transform);

            List<int> years = new List<int>();
            for (int i = fromYear; i <= toYear; i++) years.Add(i);
            ScrollYear.GetComponent<DatePickerScroll>().SetData(years, DateTime.Today.Year);
            ScrollYear.GetComponent<DatePickerScroll>().onStop = () => { SetDaysData(); };
            List<int> months = new List<int>();
            for (int i = 1; i <= 12; i++) months.Add(i);
            ScrollMonth.GetComponent<DatePickerScroll>().SetData(months, DateTime.Today.Month);
            ScrollMonth.GetComponent<DatePickerScroll>().onStop = () => { SetDaysData(); };
            SetDaysData();

            Bg.SetClick(Out);

            BtnConfirm.SetClick(() =>
            {
                int year = ScrollYear.GetComponent<DatePickerScroll>().value;
                int month = ScrollMonth.GetComponent<DatePickerScroll>().value;
                int day = ScrollDay.GetComponent<DatePickerScroll>().value;
                onConfirm?.Invoke($"{year}-{month.ToString("00")}-{day.ToString("00")}");
                Out();
            });

            Bg.transform.DOAlpha(0, false);
            Bg.transform.DOAlpha(0.8f, 0.3f, false);
            Dialog.rectTransform.anchoredPosition = new Vector2(Dialog.rectTransform.anchoredPosition.x, dialogOutY);
            Dialog.rectTransform.DOAnchorPosY(20, 0.2f).OnComplete(() =>
            {
                Dialog.rectTransform.DOAnchorPosY(0, 0.1f);
            });
        }

        private void SetDaysData()
        {
            int currYear = ScrollYear.GetComponent<DatePickerScroll>().value;
            int currMonth = ScrollMonth.GetComponent<DatePickerScroll>().value;
            int totalDays;
            switch (currMonth)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    totalDays = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    totalDays = 30;
                    break;
                default:
                    totalDays = currYear % 4 == 0 && currYear % 100 != 0 ? 29 : 28;
                    break;
            }
            List<int> days = new List<int>();
            for (int i = 1; i <= totalDays; i++) days.Add(i);
            ScrollDay.GetComponent<DatePickerScroll>().SetData(days, DateTime.Today.Day);
        }

        private void Out()
        {
            Bg.transform.DOAlpha(0, 0.1f, false);
            Dialog.rectTransform.DOAnchorPosY(dialogOutY, 0.1f).SetEase(Ease.Linear).OnComplete(() =>
            {
                Close();
                onClose?.Invoke();
            });
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }
    }
}
